package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.vo.CartBox;
import com.study.services.DemantService;
import com.study.services.OffersDetailsService;
import com.study.services.OffersService;
import com.study.services.UserService;
import com.study.services.WeixiuService;

@Controller
public class OffersController {
	@Autowired
	OffersService service;
	@Autowired
	OffersDetailsService detailService;
	@Autowired
	DemantService ds;
	@Autowired
	UserService us;
	
	/**
	 * 查看（第一次进入）
	 */
	@RequestMapping("offersty")
	@ResponseBody
	public String service1(HttpSession session,HttpServletRequest request) {
		String useid = request.getSession().getAttribute("useid").toString();
		Integer cusid = (Integer) session.getAttribute("cusview");
		List<JSONObject> list = service.selectBySel(1, 10,"",cusid,useid);
		//pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		System.out.println(json);
		session.removeAttribute("cusview");
		return json;
	}
	
	/**
	 * 进入新建
	 * @return
	 */
	@GetMapping("addoffers")
	public ModelAndView getForm2(HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/priceadd");
		mv.addObject("OfferBoxAllJson", new JSONObject());
		session.setAttribute("offer", new JSONObject());
		return mv;
	}
	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("afterSale/price")
	@ResponseBody
	public String getForm3(String input,int currentPage, int pagesize,HttpServletRequest request) {
		String useid = request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(),0,useid);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
        String json = JSONObject.toJSONString(pager);
        System.out.println("我是价格单里面的带条件查询");
		for (JSONObject jsonObject : list) {
			System.out.println(jsonObject);
		}
		return json;
	}
	
	/**
	 * 添加
	 * @return
	 */
	@PostMapping("offersform")
	@ResponseBody
	public String addForm(String theme, String custid,String linId,
			Date dates,  String price,String opp,
			String maoli, String user, String celve, String more,HttpSession session) {											  
		CartBox cart = (CartBox) session.getAttribute("OfferBoxAll");
		Integer uid = (Integer) session.getAttribute("useid");
		if (dates==null) {
			dates=new Date();
		}
		boolean result = service.addWeixiu(theme,custid,linId,dates,price,opp,maoli,uid+"",celve,more,cart);
		System.out.println(result+"我是报价的结果"+price);
		session.removeAttribute("OfferBoxAll");
		session.removeAttribute("OfferBoxAllJson");
		List<JSONObject> list = ds.selectopp(opp);
		if (list.size()==0) {
			List<JSONObject> list1 = ds.selectopp1(opp);
			if (list1.size()==0) {
				List<JSONObject> list2 = ds.selectopp2(opp);
				
				if (list2.size()==0) {
					List<JSONObject> list3 = ds.selectopp3(opp);
					if (list3.size()!=0) {
						ds.updatedem(opp);
					}
				}else {
					ds.updatesolu(opp);
				}
			}else {
				ds.updatecom(opp);
			}
		}else {
			ds.updatedd(opp);
		}
		if (result) {
				return "ok";
		} else {
			return "fail";
		}
	}
	
	/**
	 * 删除
	 * @param goo_id
	 * @return
	 */
	@PostMapping("/delOffers")
	@ResponseBody
	public String delete(String id) {
		JSONObject js = service.selectShow(id);

		if (js.getInteger("is_order")==1) {
			return "no";
		}else {
			service.delete(id);
			return "ok";
		}
		
	}
	
	/**
	 *编辑
	 * @param theme
	 * @param user
	 * @param type
	 * @param dates
	 * @param times
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @return
	 */
	@PostMapping("/editeroffersform")
	@ResponseBody
	public String editorForm(String theme,String custname,String custid,String linId,String linName,Date dates,Double price,String opp,String oppname ,
			String maoli,String user ,String celve,String more,String id,  HttpServletRequest request) {
		System.out.println(id+"编辑");
		String useid= request.getSession().getAttribute("useid").toString();
		boolean result = service.editorService(id,theme,custid,linId,dates,price,opp,useid,celve,more);
		System.out.println(result+"我是结果");
		if (result) {
				return "ok";
		} else {
			return "fail";
		}
	}
	
	/**
	 * 编辑
	 */
	@GetMapping("offerEditor")
	public ModelAndView addCustomerByColle(HttpServletRequest request,HttpSession session) {
		String parameter = request.getParameter("id");
		JSONObject offer = service.selectBycid(parameter);
		CartBox cart = detailService.selecAlltBCart(parameter);
		String useid = request.getSession().getAttribute("useid").toString();
		System.out.println(useid+"   saaa");
		//int use = Integer.getInteger(useid);
		//JSONObject js = us.selectName(use);
		//String usename = js.getString("use_name");
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		session.setAttribute("OfferBoxAll", cart);
		session.setAttribute("OfferBoxAllJson", jsonObj);
		session.setAttribute("offer", offer);
		//session.setAttribute("offerforuseName", usename);
		ModelAndView mv = new ModelAndView("beforeSale/detailmore");
		
		System.out.println(offer+"我是一条报价+详情");
		return mv;
	}
	
	/**
	 * 编辑报价单信息
	 */
	@GetMapping("offerEditor1")
	public ModelAndView addCustomerByColle1(HttpServletRequest request,HttpSession session) {
		String parameter = request.getParameter("id");
		JSONObject offer = service.selectBycid(parameter);
		CartBox cart = detailService.selecAlltBCart(parameter);
		System.out.println(cart +"             hah1");
		JSONObject jsonObj = (JSONObject) JSON.toJSON(cart);
		session.setAttribute("OfferBoxAll", cart);
		session.setAttribute("OfferBoxAllJson", jsonObj);
		session.setAttribute("offer", offer);
		ModelAndView mv = new ModelAndView("beforeSale/priceadd");
		System.out.println(offer+"我是报价详情编辑");
		return mv;
	}
	
	
	/**
	 * 选择商品返回编辑界面
	 */
	@GetMapping("offerEditor2")
	public ModelAndView addCustomerByColle2(HttpServletRequest request,HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/detailmore");
		System.out.println("我是编辑的返回");
		return mv;
	}
}
